package com.link2room.quintesson.facade.impl;

import com.link2room.common.constant.BaseConstant;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.BatchUtil;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.DateUtil;
import com.link2room.enums.exception.QuintessonSysExceptionEnum;
import com.link2room.quintesson.dto.param.manager.TaobaoXrateManagerParamDto;
import com.link2room.quintesson.dto.result.manager.TaobaoRateDataDto;
import com.link2room.quintesson.dto.result.manager.TaobaoRateInventoryPriceMap;
import com.link2room.quintesson.dto.result.manager.TaobaoXrateDto;
import com.link2room.quintesson.dto.result.manager.TaobaoXrateInventoryDto;
import com.link2room.quintesson.entity.TaobaoXhotel;
import com.link2room.quintesson.entity.TaobaoXrate;
import com.link2room.quintesson.exception.TaobaoException;
import com.link2room.optimus.dto.param.base.MarketingBaseParamDto;
import com.link2room.optimus.dto.result.base.L2REverydayPriceAndStockDto;
import com.link2room.optimus.util.MarketingUtil;
import com.link2room.quintesson.constant.QuintessonMnsConstant;
import com.link2room.quintesson.constant.QuintessonTaskNameConstant;
import com.link2room.quintesson.facade.ITaobaoXrateFacadeService;
import com.link2room.quintesson.feign.optimus.MarketingDetailFeignService;
import com.link2room.quintesson.service.ITaobaoAPIService;
import com.link2room.quintesson.util.TaobaoUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sss on 2017/4/10.
 */
@Component("taobaoXrateFacadeService")
public class TaobaoXrateFacadeServiceImpl implements ITaobaoXrateFacadeService {
    @Resource
    private ITaobaoAPIService taobaoAPIService;
    @Resource
    private ICommonService commonService;
    @Resource
    private MarketingDetailFeignService marketingDetailFeignService;

    /**
     * 飞猪产品列表
     * @param taobaoXrateManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<TaobaoXrateDto> listTaobaoXrate(TaobaoXrateManagerParamDto taobaoXrateManagerParamDto) throws Exception {
        TaobaoXrate queryTR=new TaobaoXrate();
        queryTR.setLhotelGroupCode(taobaoXrateManagerParamDto.getLhotelGroupCode());
        queryTR.setLhotelCode(taobaoXrateManagerParamDto.getLhotelCode());
        queryTR.setRmtype(taobaoXrateManagerParamDto.getRmtype());
        queryTR.setRpCode(taobaoXrateManagerParamDto.getRpCode());
        QueryResult<TaobaoXrate> taobaoXrateResult = commonService.findAllEQ(TaobaoXrate.class, queryTR, taobaoXrateManagerParamDto.getCurrentPage(), taobaoXrateManagerParamDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(taobaoXrateResult,TaobaoXrateDto.class,false);
    }

    /**
     * 飞猪产品详情
     * @param baseParamDto
     * @return
     * @throws Exception
     */
    @Override
    public TaobaoXrateDto findTaobaoXrate(BaseParamDto baseParamDto) throws Exception {
        TaobaoXrate taobaoXrate = commonService.findOneById(TaobaoXrate.class, baseParamDto.getId());
        return ClassConverUtil.copyPropertiesToDto(taobaoXrate,TaobaoXrateDto.class,false);
    }

    /**
     * 更新或保存产品列表
     * @param taobaoXrateManagerParamDto
     * @throws Exception
     */
    @Override
    public List<TaobaoXrateDto> saveOrUpdateTaobaoXrateList(TaobaoXrateManagerParamDto taobaoXrateManagerParamDto) throws Exception {
        if(taobaoXrateManagerParamDto.getTaobaoXrateDtoList()!=null&& taobaoXrateManagerParamDto.getTaobaoXrateDtoList().size()>0){
            List<TaobaoXrate> taobaoXrateList=new ArrayList<>();
            for(TaobaoXrateDto taobaoXrateDto:taobaoXrateManagerParamDto.getTaobaoXrateDtoList()){
                if(taobaoXrateDto.getId()==null){
                    String rpCode=taobaoXrateDto.getMcCode()+DefaultValue.MARK+ BaseConstant.PbChannelConstant.TAOBAO;
                    taobaoXrateDto.setRpCode(rpCode);
                    taobaoXrateDto.setHotelRpCode(MarketingUtil.getHotelRpCode(taobaoXrateDto.getLhotelCode(),rpCode));
                }
                taobaoXrateList.add(ClassConverUtil.copyProperties(taobaoXrateDto,TaobaoXrate.class));
            }
            commonService.saveOrUpdateList(taobaoXrateList);
            return ClassConverUtil.copyPropertiesListToDtoList(taobaoXrateList,TaobaoXrateDto.class,false);
        }else{
            throw new TaobaoException(BaseSysExceptionEnum.PARAM_IS_NULL,taobaoXrateManagerParamDto.toString());
        }
    }

    /**
     * 保存飞猪产品
     * @param taobaoXrateManagerParamDto
     * @throws Exception
     */
    @Override
    public TaobaoXrateDto saveOrUpdateTaobaoXrate(TaobaoXrateManagerParamDto taobaoXrateManagerParamDto) throws Exception {
        if(taobaoXrateManagerParamDto.getTaobaoXrateDto()!=null){
            TaobaoXrateDto taobaoXrateDto=taobaoXrateManagerParamDto.getTaobaoXrateDto();
            if(taobaoXrateDto.getId()==null){
                String rpCode=taobaoXrateDto.getMcCode()+DefaultValue.MARK+ BaseConstant.PbChannelConstant.TAOBAO;
                taobaoXrateDto.setRpCode(rpCode);
                taobaoXrateDto.setHotelRpCode(MarketingUtil.getHotelRpCode(taobaoXrateDto.getLhotelCode(),rpCode));
            }
            TaobaoXrate taobaoXrate = ClassConverUtil.copyProperties(taobaoXrateDto, TaobaoXrate.class);
            commonService.saveOrUpdate(taobaoXrate);
            return ClassConverUtil.copyPropertiesToDto(taobaoXrate,TaobaoXrateDto.class,false);
        }else{
            throw new TaobaoException(BaseSysExceptionEnum.PARAM_IS_NULL,taobaoXrateManagerParamDto.toString());
        }
    }

    /**
     * 上传飞猪产品列表
     * @param taobaoXrateManagerParamDto
     * @throws Exception
     */
    @Override
    public void upTaobaoXrateList(TaobaoXrateManagerParamDto taobaoXrateManagerParamDto) throws Exception {
        if(taobaoXrateManagerParamDto.getIds()!=null&& taobaoXrateManagerParamDto.getIds().size()>0){
            List<TaobaoXrateManagerParamDto> taobaoXrateManagerParamDtos =new ArrayList<>();
            for(Long id: taobaoXrateManagerParamDto.getIds()){
                TaobaoXrateManagerParamDto tbManagerParamDto=new TaobaoXrateManagerParamDto();
                tbManagerParamDto.setId(id);
                taobaoXrateManagerParamDtos.add(tbManagerParamDto);
            }
            BatchUtil.beginBatchTask(QuintessonMnsConstant.QUEUE_UP_TAOBAO_XRATE, QuintessonTaskNameConstant.QUEUE_UP_TAOBAO_XRATE, taobaoXrateManagerParamDtos);
        }else{
            throw new TaobaoException(BaseSysExceptionEnum.PARAM_IS_NULL,taobaoXrateManagerParamDto.toString());
        }
    }

    /**
     * 上传飞猪商品
     * @param taobaoXrateManagerParamDto
     * @throws Exception
     */
    @Override
    public void upTaobaoXrate(TaobaoXrateManagerParamDto taobaoXrateManagerParamDto) throws Exception {
        TaobaoXrate taobaoXrate=commonService.findOneById(TaobaoXrate.class,taobaoXrateManagerParamDto.getId());
        if(taobaoXrate!=null){
            TaobaoXrateDto taobaoXrateDto = ClassConverUtil.copyPropertiesToDto(taobaoXrate, TaobaoXrateDto.class, false);
            TaobaoUtil.setTaobaoClient(taobaoXrateManagerParamDto.getLhotelGroupCode());
            MarketingBaseParamDto marketingBaseParamDto=new MarketingBaseParamDto();
            marketingBaseParamDto.setLhotelCode(taobaoXrateDto.getLhotelCode());
            marketingBaseParamDto.setMcdCode(taobaoXrateDto.getMcdCode());
            marketingBaseParamDto.setStartDatetime(DateUtil.format(new Date(),DateUtil.webFormat));
            marketingBaseParamDto.setPbChannel(BaseConstant.PbChannelConstant.TAOBAO);
            marketingBaseParamDto.setEndDatetime(DateUtil.format(DateUtil.addDays(new Date(),90L),DateUtil.webFormat));
            QueryResult<L2REverydayPriceAndStockDto> everydayPriceAndStockDtoResult = marketingDetailFeignService.getMarketingDetailDates(marketingBaseParamDto.paramToMap()).getQueryResult();
            TaobaoRateDataDto taobaoRateDataDto = taobaoAPIService.composeTaobaoRateDataDto(everydayPriceAndStockDtoResult.getResultlist());
            taobaoXrateDto = taobaoAPIService.syncTaobaoXrate(taobaoXrateDto,taobaoRateDataDto.getTaobaoXrateInventoryDto(),taobaoRateDataDto.getRateSwitchCal());
            commonService.updateById(ClassConverUtil.copyProperties(taobaoXrateDto,TaobaoXrate.class));
        }
        BatchUtil.doBatchTaskDone(taobaoXrateManagerParamDto.getTaskMainId(),
                taobaoXrateManagerParamDto.getTaskId(),
                taobaoXrateManagerParamDto.getIsBatch());

    }

    /**
     * 上传飞猪产品批量更新
     * @param taobaoXrateManagerParamDto
     * @throws Exception
     */
    @Override
    public void upTaobaoXrateIncrementList(TaobaoXrateManagerParamDto taobaoXrateManagerParamDto) throws Exception {
        TaobaoXhotel queryTXH=new TaobaoXhotel();
        queryTXH.setLhotelGroupCode(taobaoXrateManagerParamDto.getLhotelGroupCode());
        queryTXH.setLhotelCode(taobaoXrateManagerParamDto.getLhotelCode());
        QueryResult<TaobaoXhotel> taobaoXhotelResult = commonService.findAllEQ(TaobaoXhotel.class, queryTXH);
        if(taobaoXhotelResult.getResultlist()!=null&&taobaoXhotelResult.getResultlist().size()>0){
            List<TaobaoXrateManagerParamDto> taobaoXrateManagerParamDtos =new ArrayList<>();
            for(TaobaoXhotel taobaoXhotel:taobaoXhotelResult.getResultlist()){
                TaobaoXrateManagerParamDto tbManagerParamDto=new TaobaoXrateManagerParamDto();
                tbManagerParamDto.setLhotelCode(taobaoXhotel.getLhotelCode());
                taobaoXrateManagerParamDtos.add(tbManagerParamDto);
            }
            BatchUtil.beginBatchTask(QuintessonMnsConstant.QUEUE_UP_TAOBAO_XRATE_INCREMENT, QuintessonTaskNameConstant.QUEUE_UP_TAOBAO_XRATE_INCREMENT, taobaoXrateManagerParamDtos);
        }else{
            throw new TaobaoException(BaseSysExceptionEnum.PARAM_IS_NULL,taobaoXrateManagerParamDto.toString());
        }
    }


    @Override
    public void upTaobaoXrateIncrement(TaobaoXrateManagerParamDto taobaoXrateManagerParamDto) throws Exception {
        TaobaoXrate queryTR=new TaobaoXrate();
        queryTR.setLhotelGroupCode(taobaoXrateManagerParamDto.getLhotelGroupCode());
        queryTR.setLhotelCode(taobaoXrateManagerParamDto.getLhotelCode());
        QueryResult<TaobaoXrate> taobaoXrateResult = commonService.findAllEQ(TaobaoXrate.class, queryTR);
        if(taobaoXrateResult.getResultlist()!=null&&taobaoXrateResult.getResultlist().size()>0){
            TaobaoUtil.setTaobaoClient(taobaoXrateManagerParamDto.getLhotelGroupCode());
            List<TaobaoRateInventoryPriceMap> triMapList= new ArrayList<>();
            for(TaobaoXrate taobaoXrate:taobaoXrateResult.getResultlist()){
                MarketingBaseParamDto marketingBaseParamDto=new MarketingBaseParamDto();
                marketingBaseParamDto.setLhotelCode(taobaoXrate.getLhotelCode());
                marketingBaseParamDto.setMcdCode(taobaoXrate.getMcdCode());
                marketingBaseParamDto.setPbChannel(BaseConstant.PbChannelConstant.TAOBAO);
                marketingBaseParamDto.setStartDatetime(DateUtil.format(new Date(),DateUtil.webFormat));
                marketingBaseParamDto.setEndDatetime(DateUtil.format(DateUtil.addDays(new Date(),90L),DateUtil.webFormat));
                QueryResult<L2REverydayPriceAndStockDto> everydayPriceAndStockDtoResult = marketingDetailFeignService.getMarketingDetailDates(marketingBaseParamDto.paramToMap()).getQueryResult();
                TaobaoXrateInventoryDto taobaoXrateInventoryDto = taobaoAPIService.composeIncrementTaobaoRatedata(everydayPriceAndStockDtoResult.getResultlist());

                TaobaoRateInventoryPriceMap taobaoRateInventoryPriceMap=new TaobaoRateInventoryPriceMap();
                taobaoRateInventoryPriceMap.setOut_rid(taobaoXrate.getRmtype());
                taobaoRateInventoryPriceMap.setRateplan_code(taobaoXrate.getHotelRpCode());
                taobaoRateInventoryPriceMap.setData(taobaoXrateInventoryDto);

                triMapList.add(taobaoRateInventoryPriceMap);
            }
            if(triMapList!=null&&triMapList.size()>0){
                taobaoAPIService.syncTaobaoXratesIncrementRequest(taobaoXrateManagerParamDto.getLhotelGroupCode(),triMapList);
            }
            BatchUtil.doBatchTaskDone(taobaoXrateManagerParamDto.getTaskMainId(),
                    taobaoXrateManagerParamDto.getTaskId(),
                    taobaoXrateManagerParamDto.getIsBatch());

        }else {
            throw new TaobaoException(QuintessonSysExceptionEnum.TAOBAO_RATE_NOT_EXIT);
        }
    }
}
